package com.gogowan.agroenterprise.widget;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;

public class MyProgressDialog extends ProgressDialog {


    private String loadingText;
    private TextView text;
    private Animation anim;
    private ImageView image;

    public MyProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public MyProgressDialog(Context context) {
        super(context);
    }

    public MyProgressDialog(Context context, String loadingText) {
        super(context);
        this.loadingText = loadingText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_progressdialog);
        setScreenBrightness();

        image = (ImageView) MyProgressDialog.this
                .findViewById(R.id.progress_loadingImg);
        text = (TextView) MyProgressDialog.this
                .findViewById(R.id.progress_loadingText);
        text.setText(loadingText);
        anim = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        anim.setRepeatCount(Animation.INFINITE); // 设置INFINITE，对应值-1，代表重复次数为无穷次
        anim.setDuration(1000); // 设置该动画的持续时间，毫秒单位
        anim.setInterpolator(new LinearInterpolator()); // 设置一个插入器，或叫补间器，用于完成从动画的一个起始到结束中间的补间部分
    }

    @Override
    public void onStart() {
        super.onStart();
        image.startAnimation(anim);
        text.setText(loadingText);
    }

    @Override
    public void cancel() {
        super.cancel();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @SuppressWarnings("deprecation")
    private void setScreenBrightness() {
        Window window = getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams params = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        params.width = (int) (d.getWidth() * 0.8);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        /**
         * 此处设置亮度值。dimAmount代表黑暗数量，也就是昏暗的多少，设置为0则代表完全明亮。 范围是0.0到1.0
         */
        params.dimAmount = 0;
        window.setAttributes(params);
    }

    public void setLoadingText(String loadingText) {
        this.loadingText = loadingText;
    }
}
